<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\helpers\Json;
use app\models\Pr_field;
use app\models\Uc_area;
use app\models\ProjectUploadForm;
use app\models\RaisedUploadForm;
use app\models\Pr_project;
use app\models\Rs_raised;

class UploadController extends Controller
{
    //禁用原有布局
    public $layout = "main2";
    // public $projectPlan;
    // public $applicationForm;
    // public $attachment1;
    // public $attachment2;
    // public $attachment3;
    // public $businessLicense;
    // public $externalImage;
    // public $insideImage;
    
    //显示上传创业项目
    public function actionShowProject()
    {

        RegisterController::prepare();
        $fields = $this->getField();
        $provinces = $this->getProvince();
        $cities = $this->getCity($provinces[0]->id);
        $model = new ProjectUploadForm();

        if(Yii::$app->request->isPost){
            if(null != UploadedFile::getInstance($model, 'projectPlan'))
            {
                $model->projectPlan = UploadedFile::getInstance($model, 'projectPlan');
            }
            if(null != UploadedFile::getInstance($model, 'externalImage'))
            {
                $model->externalImage = UploadedFile::getInstance($model, 'externalImage');
            }
            if(null != UploadedFile::getInstance($model, 'insideImage'))
            {
                $model->insideImage = UploadedFile::getInstance($model, 'insideImage');
            }
            if($model->validate())
            {
                if(null != $model->projectPlan){
                    $model->projectPlan->saveAs('uploads/' . $_COOKIE["user_id"] . 'project' . 'projectPlan' . '.' . $model->projectPlan->extension);
                     // $this->projectPlan = 'uploads/' . $_COOKIE["user_id"] . 'projectPlan' . '.' . $model->projectPlan->extension;
                }
                if(null != $model->externalImage){
                    $model->externalImage->saveAs('uploads/' . $_COOKIE["user_id"] . 'project' . 'externalImage' . '.' . $model->externalImage->extension);
                    // $this->externalImage = 'uploads/' . $_COOKIE["user_id"] . 'externalImage' . '.' . $model->externalImage->extension;
                }
                if(null != $model->insideImage){
                    $model->insideImage->saveAs('uploads/' . $_COOKIE["user_id"] . 'project' . 'insideImage' . '.' . $model->insideImage->extension);
                    // $this->insideImage = 'uploads/' . $_COOKIE["user_id"] . 'insideImage' . '.' . $model->insideImage->extension;
                }
                //echo '<script>alert("本页上传信息成功！")</script>';
            }
            else
            {
                echo '<script>alert("本页上传信息失败！")</script>';   
            }
        }

        return $this->render('upload-project',[
            'fields' => $fields,
            'provinces' => $provinces,
            'cities' => $cities,
            'model' => $model
            ]);
    }
    //显示上传聚募项目
    public function actionShowRaised()
    {

        RegisterController::prepare();
        $fields = $this->getField();
        $provinces = $this->getProvince();
        $cities = $this->getCity($provinces[0]->id);
        $model = new RaisedUploadForm();
        if(Yii::$app->request->isPost){
            if(null != UploadedFile::getInstance($model, 'projectPlan'))
            {
                $model->projectPlan = UploadedFile::getInstance($model, 'projectPlan');
            }
            if(null != UploadedFile::getInstance($model, 'applicationForm'))
            {
                $model->applicationForm = UploadedFile::getInstance($model, 'applicationForm');
            }
            if(null != UploadedFile::getInstance($model, 'attachment1'))
            {
                $model->attachment1 = UploadedFile::getInstance($model, 'attachment1');
            }
            if(null != UploadedFile::getInstance($model, 'attachment2'))
            {
                $model->attachment2 = UploadedFile::getInstance($model, 'attachment2');
            }
            if(null != UploadedFile::getInstance($model, 'attachment3'))
            {
                $model->attachment3 = UploadedFile::getInstance($model, 'attachment3');
            }
            if(null != UploadedFile::getInstance($model, 'businessLicense'))
            {
                $model->businessLicense = UploadedFile::getInstance($model, 'businessLicense');
            }
            if(null != UploadedFile::getInstance($model, 'externalImage'))
            {
                $model->externalImage = UploadedFile::getInstance($model, 'externalImage');
            }
            if(null != UploadedFile::getInstance($model, 'insideImage'))
            {
                $model->insideImage = UploadedFile::getInstance($model, 'insideImage');
            }
            if($model->validate())
            {
                if(null != $model->projectPlan){
                    $model->projectPlan->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'projectPlan' . '.' . $model->projectPlan->extension);
                     // $this->projectPlan = 'uploads/' . $_COOKIE["user_id"] . 'projectPlan' . '.' . $model->projectPlan->extension;
                }
                if(null != $model->applicationForm){
                    $model->applicationForm->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'applicationForm' . '.' . $model->applicationForm->extension);
                    // $this->applicationForm = 'uploads/' . $_COOKIE["user_id"] . 'applicationForm' . '.' . $model->applicationForm->extension;
                }
                if(null != $model->attachment1){
                    $model->attachment1->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'attachment1' . '.' . $model->attachment1->extension);
                    // $this->attachment1 = 'uploads/' . $_COOKIE["user_id"] . 'attachment1' . '.' . $model->attachment1->extension;
                }
                if(null != $model->attachment2){
                    $model->attachment2->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'attachment2' . '.' . $model->attachment2->extension);
                    // $this->attachment2 = 'uploads/' . $_COOKIE["user_id"] . 'attachment2' . '.' . $model->attachment2->extension;
                }
                if(null != $model->attachment3){
                    $model->attachment3->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'attachment3' . '.' . $model->attachment3->extension);
                    // $this->attachment3 = 'uploads/' . $_COOKIE["user_id"] . 'attachment3' . '.' . $model->attachment3->extension;
                }
                if(null != $model->businessLicense){
                    $model->businessLicense->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'businessLicense' . '.' . $model->businessLicense->extension);
                    // $this->businessLicense = 'uploads/' . $_COOKIE["user_id"] . 'businessLicense' . '.' . $model->businessLicense->extension;
                }
                if(null != $model->externalImage){
                    $model->externalImage->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'externalImage' . '.' . $model->externalImage->extension);
                    // $this->externalImage = 'uploads/' . $_COOKIE["user_id"] . 'externalImage' . '.' . $model->externalImage->extension;
                }
                if(null != $model->insideImage){
                    $model->insideImage->saveAs('uploads/' . $_COOKIE["user_id"] . 'raised' . 'insideImage' . '.' . $model->insideImage->extension);
                    // $this->insideImage = 'uploads/' . $_COOKIE["user_id"] . 'insideImage' . '.' . $model->insideImage->extension;
                }
                //echo '<script>alert("本页上传信息成功！")</script>';
            }
            else
            {
                echo '<script>alert("本页上传信息失败！")</script>';   
            }
        }

        return $this->render('upload-raised',[
            'fields' => $fields,
            'provinces' => $provinces,
            'cities' => $cities,
            'model' => $model
            ]);
    }
    //插入聚募项目到数据库
    public function actionInsertRaised($title, $field, $province, $city, $leaderName, $mobile, $email, $weixin, $projectPlan, $applicationForm, $attachment1, $attachment2, $attachment3, $businessLicense, $externalImage, $insideImage)
    {
        $raised = new Rs_raised();
        $raised->user_id = $_COOKIE["user_id"];
        $raised->title = $title;
        $raised->type = -1;
        $date = date('Y-m-d H:i:s');
        $raised->createdDate = $date;
        $raised->field = $field;
        $raised->area = $province.' '.$city;
        $raised->charge_name = $leaderName;
        $raised->charge_mobile = $mobile;
        $raised->charge_email = $email;
        $raised->charge_wechat = $weixin;
        $raised->resources = $projectPlan.";".
        $applicationForm.";".
        $attachment1.";".
        $attachment2.";".
        $attachment3.";".
        $businessLicense.";".
        $externalImage.";".
        $insideImage;
        $raised->save();
        //return Json::encode($raised);
    }
    //插入创业项目到数据库
    public function actionInsertProject($title, $field, $province, $city, $leaderName, $mobile, $email, $weixin, $description, $market, $merits, $business, $finance, $member, $demand, $projectPlan, $externalImage, $insideImage)
    {
        $project = new Pr_project();
        $project->user_id = $_COOKIE["user_id"];
        $project->title = $title;
        $project->status = -1;
        $date = date('Y-m-d H:i:s');
        $project->createdDate = $date;
        $project->field = $field;
        $project->area = $province.' '.$city;
        $project->charge_name = $leaderName;
        $project->charge_mobile = $mobile;
        $project->charge_email = $email;
        $project->charge_wechat = $weixin;
        $project->description = $description;
        $project->market = $market;
        $project->merits = $merits;
        $project->business = $business;
        $project->finance = $finance;
        $project->need = $member;
        $project->demand = $demand;
        $project->resources = $projectPlan.";".
        $externalImage.";".
        $insideImage;
        $project->save();
        return Json::encode($project);
    }
    //项目类型数据
    public function getField()
    {
        $query = Pr_field::find()
        ->orderBy('field_id')
        ->all();
        
        return $query;
    }
    //项目所在地省份
    public function getProvince()
    {
        $query = Uc_area::find()
        ->where([
            'father_id' => -1,
            'del_flag' => 0
            ])
        ->orderBy('id')
        ->all();
        
        return $query;
    }
    //项目所在地城市
    public function getCity($father_id)
    {
        $query = Uc_area::find()
        ->where([
            'father_id' => $father_id,
            'del_flag' => 0
            ])
        ->orderBy('id')
        ->all();
        
        return $query;
    }
    //上传项目
    /*
    public function actionUpload()
    {
        $request = Yii:$app->request;
        $title = $request->post("title");
        $avatar = $request->post("avatar");
        $description = $request->post("dexcription");

        $project = new Pr_project();
        $project->title = $title;
        $project->avatar = $avatar;
        $project->description = $description;
        
        if($project->save() > 0)
        {
            return $this->render('homePage',['project' => $project]);
        }
        else
        {
            return $this->render('error');
        }
    }*/
    //项目所在地城市
    public function actionCity($father_id)
    {
        $query = Uc_area::find()
        ->where([
            'father_id' => $father_id,
            'del_flag' => 0
            ])
        ->orderBy('id')
        ->all();
        return Json::encode($query);
    }
}
